#include "udiskwidget.h"

#define ICON_UDISK "background-image:url(:/resources/udisk.png);background-attachment:fixed;background-position:center;background-repeat:no-repeat;"
#define UDISK_EVENT "/sys/devices/virtual/android_usb/android0/state"
#define UDISK_HIDDEN "/oem/yiting/.udisk"
#define CONNECT_PC_STATUS  "CONFIGURED\n"

UDiskWidget::UDiskWidget()
{    
    statusBarWindow = new QWidget(rootWindow);
    statusBarWindow->setGeometry(QRect(0, 0, rootSize.width(), VIEW_STATE_BAR_HEIGH));
    statusBarWindow->setStyleSheet("background-color:#000;");

    statusBar = new StatusBar(statusBarWindow);
    connect(statusBar, &StatusBar::statusUpdate, this, [&]{statusBar->updateStatus();});
    connect(statusBar, &StatusBar::updateBT, this, [&](bool status){statusBar->statusWidget->updateBtStatus(status);});
    connect(statusBar, &StatusBar::updateWiredHeadSet, this, [&](bool status){statusBar->statusWidget->updateWiredHeadSetStatus(status);});

    QLabel *icon = new QLabel;
    icon->setStyleSheet(ICON_UDISK);
    icon->setFixedSize(400, 230);

    text = new QLabel;
    text->setText("已连接电脑");
    text->setFixedSize(400, 50);
    text->setStyleSheet("QLabel{color:#fff;font-size:30px;}");
    text->setAlignment(Qt::AlignCenter);

    QVBoxLayout *main_layout = new QVBoxLayout(rootWindow);
    main_layout->setMargin(0);
    main_layout->setSpacing(0);
    main_layout->addSpacing(50);
    main_layout->addStretch(1);
    main_layout->addWidget(icon, 0, Qt::AlignCenter);
    main_layout->addWidget(text, 0, Qt::AlignCenter);
    main_layout->addStretch(1);
}

void UDiskWidget::KeyEventDeal(int , int )
{
}

void UDiskWidget::toShow()
{
    statusBar->Start();
    Show("");
    this->activateWindow();
    this->raise();

    //锁键
    T_Screen cmd;
    cmd.msg_type = SCREEN_MSG_CMD_CLOSE;
    MsgScreenSend(&cmd, SCREEN_MSG_ID_QMSGSERVER);
}

void UDiskWidget::toHide()
{
    statusBar->Stop();
    Hide();

    T_Screen cmd;
    cmd.msg_type = SCREEN_MSG_CMD_OPEN;
    MsgScreenSend(&cmd, SCREEN_MSG_ID_QMSGSERVER);
}

UDiskManager::UDiskManager()
{
    widget = new UDiskWidget;
    widget->Hide();
    connect(this, &UDiskManager::toShow, widget, &UDiskWidget::toShow);
    connect(this, &UDiskManager::toHide, widget, &UDiskWidget::toHide);
}

void UDiskManager::udiskCheck()
{
    int fd = open(UDISK_HIDDEN,O_RDONLY);
    if(fd >= 0)
    {
        ::close(fd);
        if(!widget->isHidden())
        {
            WELOG_DEBUG() << "udisk hide";
            emit toHide();
            T_Plan data;
            data.msg_cmd = PLAN_MSG_CMD_TIMER_RESET;
            data.msg_text[0] = QT_EVENT_STOP;
            WELOG_DEBUG() << MsgPlanSend(&data, PLAN_MSG_ID_QSERVER);
        }
    }
    else
    {
        char buf[20] = {0};
        bool isConnect = false;
        int fd = open(UDISK_EVENT,O_RDONLY);
        if(fd >= 0)
        {
            int ret = read(fd,buf,1024);
            if(ret > 0)
            {
                if(strcmp(buf, CONNECT_PC_STATUS) ==0)
                {
                    if(widget->isHidden())
                    {
                        WELOG_DEBUG() << "udisk show";
                        emit toShow();
                        T_Plan data;
                        data.msg_cmd = PLAN_MSG_CMD_TIMER_RESET;
                        data.msg_text[0] = QT_EVENT_START;
                        WELOG_DEBUG() << MsgPlanSend(&data, PLAN_MSG_ID_QSERVER);
                    }
                    isConnect = true;
                }
            }
            ::close(fd);
        }
        if(!isConnect)
        {
            if(!widget->isHidden())
            {
                WELOG_DEBUG() << "udisk hide";
                emit toHide();
                T_Plan data;
                data.msg_cmd = PLAN_MSG_CMD_TIMER_RESET;
                data.msg_text[0] = QT_EVENT_STOP;
                WELOG_DEBUG() << MsgPlanSend(&data, PLAN_MSG_ID_QSERVER);
            }
        }
    }
}
